<template>
  <view class="login-container">
    <view class="login-box">
      <view class="title">用户登录</view>
      <view class="input-group">
        <input type="text" v-model="loginForm.username" placeholder="请输入用户名" />
      </view>
      <view class="input-group">
        <input type="password" v-model="loginForm.password" placeholder="请输入密码" />
      </view>
      <view class="btn-group">
        <button class="login-btn" @click="handleLogin">登录</button>
        <button class="register-btn" @click="goToRegister">注册账号</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    async handleLogin() {
      if (!this.loginForm.username || !this.loginForm.password) {
        uni.showToast({
          title: '请输入用户名和密码',
          icon: 'none'
        });
        return;
      }
      
      try {
        const res = await uni.request({
          url: 'http://localhost:8080/login',
          method: 'POST',
          data: this.loginForm,
          header: {
            'Content-Type': 'application/json'
          }
        });
        
        if (res[1].data.code === 2000) {
          // 登录成功，保存token
          uni.setStorageSync('token', res[1].data.data.token);
          uni.showToast({
            title: '登录成功',
            icon: 'success'
          });
          // 跳转到首页
          setTimeout(() => {
            uni.reLaunch({
              url: '/pages/index/index'
            });
          }, 1500);
        } else {
          uni.showToast({
            title: res[1].data.msg || '登录失败',
            icon: 'none'
          });
        }
      } catch (e) {
        console.error(e);
        uni.showToast({
          title: '登录失败，请稍后重试',
          icon: 'none'
        });
      }
    },
    goToRegister() {
      uni.navigateTo({
        url: '/pages/register/register'
      });
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  
  .login-box {
    width: 80%;
    padding: 30rpx;
    background-color: #fff;
    border-radius: 20rpx;
    box-shadow: 0 0 20rpx rgba(0,0,0,0.1);
    
    .title {
      font-size: 40rpx;
      font-weight: bold;
      text-align: center;
      margin-bottom: 50rpx;
    }
    
    .input-group {
      margin-bottom: 30rpx;
      
      input {
        width: 100%;
        height: 80rpx;
        padding: 0 20rpx;
        border: 1px solid #ddd;
        border-radius: 10rpx;
        font-size: 28rpx;
      }
    }
    
    .btn-group {
      margin-top: 50rpx;
      
      button {
        width: 100%;
        height: 80rpx;
        line-height: 80rpx;
        text-align: center;
        border-radius: 10rpx;
        font-size: 32rpx;
        margin-bottom: 20rpx;
      }
      
      .login-btn {
        background-color: #007AFF;
        color: #fff;
      }
      
      .register-btn {
        background-color: #fff;
        color: #007AFF;
        border: 1px solid #007AFF;
      }
    }
  }
}
</style> 